<template>
  <div class="custom-titlebar" :class="{ 'draggable': isDraggable }">
    <!-- 左侧应用信息 -->
    <div class="titlebar-left">
      <div class="app-icon">
        <img :src="logo" alt="Logo" />
      </div>
      <div class="app-title">美镓传感</div>
    </div>
    
    <!-- 中间拖拽区域 -->
    <div class="titlebar-center drag-region"></div>
    
    <!-- 右侧控制按钮 -->
    <div class="titlebar-right">
      <!-- 最小化按钮 -->
      <div class="control-btn minimize-btn" @click="minimizeWindow" title="最小化">
        <el-icon><Minus /></el-icon>
      </div>
      
      <!-- 最大化/还原按钮 -->
      <div class="control-btn maximize-btn" @click="toggleMaximize" :title="isMaximized ? '还原' : '最大化'">
        <el-icon v-if="!isMaximized"><FullScreen /></el-icon>
        <el-icon v-else><CopyDocument /></el-icon>
      </div>
      
      <!-- 关闭按钮 -->
      <div class="control-btn close-btn" @click="closeWindow" title="关闭">
        <el-icon><Close /></el-icon>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { Minus, FullScreen, CopyDocument, Close } from '@element-plus/icons-vue'
import logoImg from '@/assets/logo.png'

const isDraggable = ref(true)
const isMaximized = ref(false)
const logo = logoImg // 使用import导入的图片路径
// 最小化窗口
const minimizeWindow = () => {
  if (window.electronAPI && window.electronAPI.minimizeWindow) {
    window.electronAPI.minimizeWindow()
  }
}

// 切换最大化状态
const toggleMaximize = () => {
  if (window.electronAPI && window.electronAPI.toggleMaximize) {
    window.electronAPI.toggleMaximize()
    isMaximized.value = !isMaximized.value
  }
}

// 关闭窗口
const closeWindow = () => {
  if (window.electronAPI && window.electronAPI.closeWindow) {
    window.electronAPI.closeWindow()
  }
}

onMounted(() => {
  // 监听窗口最大化状态变化
  if (window.electronAPI && window.electronAPI.onWindowStateChange) {
    window.electronAPI.onWindowStateChange((state: { isMaximized: boolean }) => {
      isMaximized.value = state.isMaximized
    })
  }
})
</script>

<style scoped lang="less">
.custom-titlebar {
  height: 40PX!important;
  min-height: 40PX!important;
  background: linear-gradient(135deg, #1a2332 0%, #0f1419 100%);
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1PX solid rgba(14, 253, 255, 0.2);
  position: relative;
  z-index: 1000;
  user-select: none;
  
  &.draggable {
    -webkit-app-region: drag;
  }
}

.titlebar-left {
  display: flex;
  align-items: center;
  padding-left: 12PX !important;
  -webkit-app-region: no-drag;
  
  .app-icon {
    width: 24PX !important;
    height: 24PX !important;
    margin-right: 10PX !important;
    
    img {
      width: 100%;
      height: 100%;
      object-fit: contain;
    }
  }
  
  .app-title {
    font-size: 14PX !important;
    color: #fff;
    font-weight: 500;
    text-shadow: 0 1PX 2PX rgba(0, 0, 0, 0.3);
  }
}

.titlebar-center {
  flex: 1;
  height: 100%;
  
  &.drag-region {
    -webkit-app-region: drag;
  }
}

.titlebar-right {
  display: flex;
  align-items: center;
  height: 100%;
  -webkit-app-region: no-drag;
}

.control-btn {
  width: 50PX !important;
  height: 40PX !important;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: background-color 0.2s ease;
  
  .el-icon {
    font-size: 16PX;
    color: #b0c4de;
  }
  
  &:hover {
    background-color: rgba(255, 255, 255, 0.1);
    
    .el-icon {
      color: #fff;
    }
  }
  
  &.close-btn:hover {
    background-color: #e74c3c;
    
    .el-icon {
      color: #fff;
    }
  }
  
  &.minimize-btn:hover {
    background-color: rgba(29, 233, 182, 0.2);
  }
  
  &.maximize-btn:hover {
    background-color: rgba(29, 200, 233, 0.2);
  }
}


</style>